package course.cmsc436.medicationmanager;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import course.cmsc436.medicationmanager.NDCFetcher.FetchCompleteListener;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainScanActivity extends Activity implements OnClickListener, FetchCompleteListener {

	private Button scanBtn, searchBtn;
	private TextView formatTxt, contentTxt;
	private String scanContent ;
	private String scanFormat;
	private String ndcCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scan_main);

		scanBtn = (Button)findViewById(R.id.scan_button);
		searchBtn = (Button)findViewById(R.id.button_search_drug);
		formatTxt = (TextView)findViewById(R.id.scan_format);
		contentTxt = (TextView)findViewById(R.id.scan_content);

		scanBtn.setOnClickListener(this);

		searchBtn.setEnabled(false);
	}


	public void onClick(View v){
		//respond to clicks
		if(v.getId()==R.id.scan_button){
			//scan
			IntentIntegrator scanIntegrator = new IntentIntegrator(this);
			scanIntegrator.initiateScan();
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		//retrieve scan result
		IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

		if (scanningResult != null) {
			//we have a result
			String scanContent = scanningResult.getContents();

			if(scanContent.length()<11){ // for now a basic check of the input
				Toast toast = Toast.makeText(getApplicationContext(),
						"invalid NDC BARCODE!", Toast.LENGTH_SHORT);
				toast.show();

				String scanFormat = scanningResult.getFormatName();

				formatTxt.setText("FORMAT: " + scanFormat);
				contentTxt.setText("CONTENT: " + scanContent);
			}
			ndcCode = scanContent;
			String scanFormat = scanningResult.getFormatName();
			formatTxt.setText("FORMAT: " + scanFormat);
			contentTxt.setText("CONTENT: " + scanContent);

			searchBtn.setEnabled(true);
		}else{
			Toast toast = Toast.makeText(getApplicationContext(),
					"No scan data received!", Toast.LENGTH_SHORT);
			toast.show();
		}

	}

	public void onClickSearchDrug(View v){
		//http://www.tevagenerics.com/default.aspx?pageid=4090

		//		Log.i("scanContent",scanContent);
		//		String ndcCode = convertToNDC(scanContent);
		//		
		//		Log.i("ndcCode",ndcCode);

		
		NDCFetcher.doAsync(ndcCode, this);

	}

	public void onFetchComplete(String s) {
		contentTxt.setText(s);
	}


	private String convertToNDC (String value ){
		StringBuffer returnVal = new StringBuffer( value);

		if (value.length()<11){// 
			return  value;
		}


		returnVal.deleteCharAt(0);
		returnVal.deleteCharAt(returnVal.length()-1);


		returnVal.insert(4, '-');
		returnVal.insert(9, '-');
		return returnVal.toString();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}